package io.xccit.hotelsystem.ui.hotel;

import android.content.Context;
import android.database.Cursor;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;

import io.xccit.hotelsystem.R;
import io.xccit.hotelsystem.ui.home.HomeFragment;
import io.xccit.hotelsystem.utils.SQLiteHelper;

/**
 * 酒店ViewModel
 */
public class HotelViewModel extends ViewModel {
    private final MutableLiveData<List<HomeFragment.Hotel>> hotels = new MutableLiveData<>();
    private Context context;
    
    public HotelViewModel(Context context) {
        this.context = context;
    }
    
    public LiveData<List<HomeFragment.Hotel>> getHotels() {
        return hotels;
    }

    /**
     * 豪华酒店
     */
    public void loadLuxuryHotels() {
        List<HomeFragment.Hotel> hotelList = new ArrayList<>();
        SQLiteHelper dbHelper = new SQLiteHelper(context);
        Cursor cursor = dbHelper.getAllHotels();
        
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String name = cursor.getString(1);
                String desc = cursor.getString(2);
                String price = cursor.getString(3);
                String image = cursor.getString(4);
                
                // 这里需要根据实际图片资源处理
                int imageRes = R.drawable.hotel_home_1;
                if (image.equals("hotel_home_2")) imageRes = R.drawable.hotel_home_2;
                else if (image.equals("hotel_home_3")) imageRes = R.drawable.hotel_home_3;
                else if (image.equals("hotel_home_4")) imageRes = R.drawable.hotel_home_4;
                
                hotelList.add(new HomeFragment.Hotel(imageRes, name, desc, price));
            } while (cursor.moveToNext());
        }
        cursor.close();
        hotels.setValue(hotelList);
    }

    /**
     * 商务酒店
     */
    public void loadBusinessHotels() {
        List<HomeFragment.Hotel> businessHotels = new ArrayList<>();
        businessHotels.add(new HomeFragment.Hotel(R.drawable.hotel_home_3, "商务会议中心酒店", "配备完善会议设施，适合商务出行", "800"));
        businessHotels.add(new HomeFragment.Hotel(R.drawable.hotel_home_4, "城市快捷商务酒店", "交通便利，提供免费WiFi和办公区", "600"));
        hotels.setValue(businessHotels);
    }

    /**
     * 经济酒店
     */
    public void loadEconomyHotels() {
        List<HomeFragment.Hotel> economyHotels = new ArrayList<>();
        economyHotels.add(new HomeFragment.Hotel(R.drawable.hotel_home_1, "经济型连锁酒店", "干净舒适的基础住宿选择", "300"));
        economyHotels.add(new HomeFragment.Hotel(R.drawable.hotel_home_2, "青年旅舍", "背包客的理想选择，共享空间节省开支", "1500"));
        hotels.setValue(economyHotels);
    }

    /**
     * 快捷酒店
     */
    public void loadQuickHotels() {
        List<HomeFragment.Hotel> quickHotels = new ArrayList<>();
        quickHotels.add(new HomeFragment.Hotel(R.drawable.hotel_home_3, "快捷酒店A", "24小时前台服务，即到即住", "180"));
        quickHotels.add(new HomeFragment.Hotel(R.drawable.hotel_home_4, "快捷酒店B", "简约风格，经济实惠", "180"));
        hotels.setValue(quickHotels);
    }

    private final MutableLiveData<List<HomeFragment.Hotel>> moreHotels = new MutableLiveData<>();
    
    public LiveData<List<HomeFragment.Hotel>> getMoreHotels() {
        return moreHotels;
    }

    /**
     * 推荐更多数据加载
     */
    public void loadMoreHotels() {
        List<HomeFragment.Hotel> moreHotelList = new ArrayList<>();
        moreHotelList.add(new HomeFragment.Hotel(R.drawable.fast_hotel_1, "绿园宾馆", "环境优美，靠近市中心", "280"));
        moreHotelList.add(new HomeFragment.Hotel(R.drawable.fast_hotel_2, "温馨客栈", "家庭式服务，温馨舒适", "320"));
        moreHotelList.add(new HomeFragment.Hotel(R.drawable.fast_hotel_3, "快捷酒店C", "经济实惠，干净整洁", "180"));
        moreHotelList.add(new HomeFragment.Hotel(R.drawable.fast_hotel_4, "商务快捷酒店", "配备小型会议室", "350"));
        moreHotelList.add(new HomeFragment.Hotel(R.drawable.fast_hotel_5, "城市便捷酒店", "交通便利，服务周到", "300"));
        moreHotelList.add(new HomeFragment.Hotel(R.drawable.fast_hotel_6, "青年旅舍分店", "背包客的理想选择", "150"));
        moreHotels.setValue(moreHotelList);
    }
    
    public HotelViewModel() {
    }
}